<?php
## include required files
/*******************************/
require_once '../../includefiles.php';
include_once("../../model/users.php");
require_once '../../model/budget.php';
require_once '../../model/trade.php';
require_once '../../redirect.php';
require_once '../../model/state.php';
## Create Objects
/*******************************/
$userObj = new Model_Users();
$budgetObj = new Model_Budget();
$objTrade = new Model_TRADE();
$stateObj = new Model_State();
/*******************************/

checkUserLogin();

if($_SESSION['user_type'] == '4')
{
	@header("location:".SITE_URL."/");
	exit;
}

$userId = $_SESSION['getbids_id'];
$userDetArray = $userObj->getUserDetailsByUserId($userId);
$smarty->assign('userDetArray',$userDetArray);

$job_id = base64_decode($_GET['id1']);	
$job_details = $userObj->getJobDetailsById($job_id);
$smarty->assign('job_details', $job_details);


## Fetch all active states
$stateArray = $stateObj->getAllStateWithActiveStatus();
//echo "<pre>"; print_r($stateArray); exit;
$smarty->assign('stateArray', $stateArray);

$getallradiusarea = $userObj->getAllLongLatByUserID($userId);
$smarty->assign('latlongArray',$latlongArray);


$tradejobtypeArray = $objTrade->getAllJobTypeWithActiveStatus();
foreach($tradejobtypeArray as $tradejobtypeArray)
{	
	$newarray[] =	$tradejobtypeArray['parent_id'];
}
	
$tradecategoryArray = array_values(array_unique($newarray));
if($tradecategoryArray[0]!='')
{
		$imopldeids = @implode(",",$tradecategoryArray);
		$newtradecatArray = $objTrade->getAllTradesCategoryWithmultipleId($imopldeids);
}
//echo "<pre>";print_R($tradecatArray);die();
$smarty->assign("newtradecategoryArray", $newtradecatArray);

$jobtypeArray = $objTrade->getAllJobtypeByTradeId($job_details['trade_id']);
$smarty->assign("jobtypeArray", $jobtypeArray);

$getallactivebudgetArray = $budgetObj->getAllBudgetWithActiveStatus();
$smarty->assign("getallactivebudgetArray", $getallactivebudgetArray);


if(trim($_POST['trade']) != '' && trim($_POST['job_desc']) != '' && trim($_POST['work_to_start']) != '') 
{
		extract($_POST);


		$job_id = $_POST['hidden_jobid'];
		$userPostArray = array();
		$userPostArray["trade_id"] 			= $_POST['trade'];
		if($_POST['jobtype']!='')
			$userPostArray["jobtype_id"]		= $_POST['jobtype'];					
		$userPostArray["job_desc"] 			= $_POST['job_desc'];
		$userPostArray['workstart'] 		= $_POST['work_to_start'];	
		$userPostArray['approx_budget']		= $_POST['approx_budget'];						
		$userPostArray['area'] 				= $_POST['postcode'];			
		$userPostArray['state_id'] 				= $_POST['state_id'];
		
		$err = false;
		if($_FILES['joblogo']['name'] != '')
		{
			$size =	getimagesize($_FILES['joblogo']['tmp_name']);
			if($size[0] >= JOBIMAGEWIDTH && $size[1] >= JOBIMAGEHEIGHT) 
			{   
				@unlink('../../uploads/job/'.$job_details['image']);
				$path = '../../uploads/job/'; 
				$imageName    = $_FILES['joblogo']['name']; 

				$arrImageName = explode(".",$imageName);
				$position     = count($arrImageName);
				
				$fileName     = date('Ymdhis').".".$arrImageName[$position-1]; 
				@copy($_FILES['joblogo']['tmp_name'], $path.$fileName);
				
				$userPostArray['image']	= $fileName;

			}
			else
			{
			   $err = true;				   
			}
		}
		if($err == false)
		{
			$userObj->editJobPostValueByJOBId($userPostArray, $job_id);
			$_SESSION['msg'] = "<div class='success_msg'><span>Job updated successfully.</span></div>";
			redirect_to_link(SITE_URL.'/jobs/myjobs/');
			exit;
		}
		else
		{
				$_SESSION['msg'] = "<div class='error_msg'><span>".JOBIMAGE_WIDTHHEIGHT."</span></div>";
		}
	
}


if($_SESSION['msg'] != ''){
	$smarty->assign("msg", $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

$active ="myaccount";
$smarty->assign('active',$active);
$subactive ="myjobs";
$smarty->assign('subactive',$subactive);
$smarty->assign('mainmenu',"4");


$smarty->display(TEMPLATEDIR . '/modules/jobs/editjob.tpl');
?>